package 博客Test;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;

public class Test3 {
    public static void main(String[] args) {
        Animal a1 = new Animal("真兽宝一号",13);
        Animal a2 = new Animal("真兽宝二号",15);
        Animal a3 = new Animal("真兽宝三号",23);
        Animal a4 = new Animal("真兽宝四号",25);
        ArrayList<Animal> list = new ArrayList<>();
        Collections.addAll(list,a1,a2,a3,a4);

        // 匿名内部类
        Collections.sort(list, new Comparator<Animal>() {

            @Override
            public int compare(Animal animal1, Animal animal2) {
                System.out.println("我是comparator接口中的compare方法" +
                                   "我不满足compareable接口中的compareTo方法," +
                                   "我要重新定义比较规则!");
                return Integer.compare(animal1.age,animal2.age);
            }
        });

        // lambda 表达式遍历(想要了解更多? 关注博主,后续更加精彩!)
        list.forEach(s-> System.out.println(s.name+" "+s.age));
    }
}





// 这里给大家随便测几组
/*        System.out.println(compare.compare(a1,a2));// -1
        System.out.println(compare.compare(a3,a2));// 1
        System.out.println(compare.compare(a4,a4));// 0*/
